<template>
  <div class="user-history-did">
    <el-card>
      <el-form label-width="80px" label-position="right">
        <h2>历史题目列表</h2>
        <el-divider></el-divider>
        <el-table :data="historyData" height="400" border style="width: 100%">
          <el-table-column prop="modify_time" label="日期" width="150">
          </el-table-column>
          <el-table-column prop="title" label="题目"> </el-table-column>
          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button
                @click="onCheck(scope.row)"
                type="text"
                icon="el-icon-search"
              >
                查看
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>

      <el-dialog
        title="题目详情"
        :visible.sync="dialogVisible"
        :before-close="beforeDialogClose"
        append-to-body
      >
        <el-form :model="problem" label-width="80px">
          <el-form-item label="题目名称">
            <div
              style="
                background-color: #f6f6f6;
                padding: 0 20px;
                border-radius: 4px;
              "
            >
              {{ this.problem.title }}
            </div>
          </el-form-item>
          <el-form-item label="题目难度">
            <el-rate
              v-if="problem.difficulty"
              v-model="problem.difficulty"
              :max="3"
              :texts="['简单', '中等', '困难']"
              show-text
              disabled
            ></el-rate>
            <el-tag v-else disable-transitions>待定</el-tag>
          </el-form-item>
          <el-form-item label="题目描述">
            <div
              style="
                background-color: #f6f6f6;
                padding: 0 20px;
                border-radius: 4px;
              "
            >
              {{ this.problem.content }}
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer">
          <el-button type="primary" icon="el-icon-edit" @click="onContinue">
            继续做题
          </el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import axios from "axios";
import timeZone from "time-zone";

export default {
  name: "UserHistoryDid",
  created() {
    this.getProblemData();
  },
  data() {
    return {
      dialogVisible: false,
      historyData: [],
      problem: {
        id: "",
        title: "",
        difficulty: 0,
        content: "",
        status: 0,
      },
    };
  },
  methods: {
    setProblemDefault() {
      this.problem = {
        _id: "",
        title: "",
        difficulty: 0,
        content: "",
        status: 0,
      };
    },
    beforeDialogClose(done) {
      this.setProblemDefault();
      done();
    },
    getProblemData() {
      axios
        .get("/api/user/problemdone")
        .then((response) => {
          console.log(response);
          let time_bias_str = timeZone();
          let time_bias = 0;
          if (time_bias_str.substr(0, 1) === "+") {
            time_bias += Number(time_bias_str.substr(1, 1));
          } else if (time_bias_str.substr(0, 1) === "-") {
            time_bias -= Number(time_bias_str.substr(1, 1));
          }
          this.historyData = response.data.result.problemSet;
          this.historyData.forEach((data) => {
            data.modify_time =
              data.modify_time.substr(0, 10) +
              " " +
              (Number(data.modify_time.substr(11, 2)) + time_bias) +
              data.modify_time.substr(13, 6);
            data.title = data.problem[0].title;
            data.difficulty = data.problem[0].difficulty;
            data.content = data.problem[0].content;
            data._id = data.problem[0]._id;
          });
          this.$message.success("获取历史信息成功");
        })
        .catch((error) => {
          console.log(error);
          this.$message.error("获取历史信息失败");
        });
    },
    onCheck(row) {
      this.problem = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },
    onContinue() {
      this.dialogVisible = false;
      let pid = this.problem._id;
      this.setProblemDefault();
      this.$router.push("/problem/" + pid);
    },
  },
};
</script>

<style>
/* .small-box {
  border: 1px solid #dcdfe6;
  width: 600px;
  margin: 180px auto;
  padding: 15px 15px 15px 15px;
  border-radius: 5px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  background: rgb(255, 255, 255);
  text-align: center;
  position: absolute;
  top: -10px;
  left: -800px;
  right: 0px;
} */
</style>
